package com.fh.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.dto.PurPurview;
import com.fh.dto.SysUser;
import com.fh.web.constants.GlobalConstants;
import com.fh.web.service.CustomService;
import com.fh.web.service.PurviewService;
import com.fh.web.util.TreeNodeUtil;
import com.fh.web.vo.JsonTreeData;
import com.fh.web.vo.SubSysParamVo;

/**
 * 用户管理controller
 * 
 * @author admin
 *
 */
@Controller
@RequestMapping("user")
public class CustomController {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private CustomService customService;
	@Resource
	private PurviewService purviewService;

	

	/**
	 * 根据用户的信息获取权限
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("userPurViewList")
	public List<JsonTreeData> userPurViewList(HttpServletRequest request, HttpServletResponse response) {
		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(GlobalConstants.SYS_USER);
		// 申明easyUi前台显示树参数
		List<JsonTreeData> jsonListReturn = new ArrayList<JsonTreeData>();
		List<JsonTreeData> treeDataList = new ArrayList<JsonTreeData>();
		try {
			// 获取session用户信息
			// 查看用户所有的权限点
			List<PurPurview> purviewList = customService.getUserPurview(sysUser);

			if ((purviewList != null) && (purviewList.size() > 0)) {
				/*
				 * 为了整理成公用的方法，所以将查询结果进行二次转换。 其中id为主键ID parentid为父ID text为节点名称
				 */
				for (PurPurview r : purviewList) {
					JsonTreeData treeData = new JsonTreeData();
					treeData.setId(r.getPurId() + "");
					treeData.setPid(r.getParentPurId() + "");
					treeData.setText(r.getPurName());
					treeData.setUrl(r.getPurValue());
					treeData.setPurCode(r.getPurCode());
					treeData.setPurType(r.getPurType());
					treeDataList.add(treeData);
				}
				// 最后得到结果集,经过FirstJSON转换后就可得所需的json格式
				treeDataList = TreeNodeUtil.getfatherNode(treeDataList);
				TreeNodeUtil.detailJsonTreeData(jsonListReturn, treeDataList);
			}
		} catch (Exception e) {
			logger.error("获取登录后，可看到的菜单异常", e);
			return null;
		}

		return jsonListReturn;

	}

	/**
	 * 跳转到主页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("index")
	public String index1(HttpServletRequest request, HttpServletResponse response) {
		logger.info("main  ");
		return "index/index";
	}

	/**
	 * 跳转到单点登录模块
	 * 
	 * @param request
	 * @param response
	 * @return module
	 * @throws Exception
	 * @throws NumberFormatException
	 */
	@RequestMapping("module")
	public ModelAndView module(HttpServletRequest request, HttpServletResponse response)
			throws NumberFormatException, Exception {
		String purCode = request.getParameter("purCode");
		Map<String, Object> resMap = new HashMap<String, Object>();
		SubSysParamVo vo = purviewService.getPurview(purCode);
		if (null != vo) {
			resMap.put("subSysParam", vo);
			return new ModelAndView("index/module", resMap);
		} else {
			resMap.put("purCode", purCode);
			return new ModelAndView("index/addModule", resMap);
		}

	}


	/**
	 * 添加单点登录信息
	 * 
	 * @param vo
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("addModule")
	public SubSysParamVo addModule(SubSysParamVo vo, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SubSysParamVo role = new SubSysParamVo();
		// 判断排序是否重复
		int check = purviewService.checkSubSysSort(vo);
		if (check == 0) {
			int num = purviewService.addModule(vo);
			// 成功返回状态0
			if (num > 0) {
				role.setState(1);
			} else {
				role.setState(0);
			}
		} else {
			role.setState(2);
		}
		return role;
	}

	/**
	 * 修改单点登录信息
	 * 
	 * @param vo
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("updateModule")
	public SubSysParamVo updateModule(SubSysParamVo vo, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 判断排序是否重复
		SubSysParamVo role = new SubSysParamVo();
		int check = purviewService.checkSubSysSort(vo);
		if (check == 0) {
			int num = purviewService.updateModule(vo);
			// 成功返回状态0
			if (num > 0) {
				role.setState(1);
			} else {
				role.setState(0);
			}
		} else {
			role.setState(2);
		}
		return role;
	}

	/**
	 * 修改密码
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("updatePassword")
	public int updatePassword(HttpServletRequest request, HttpServletResponse response) {
		String oldPassword = request.getParameter("oldPassword");
		String newdPassword = request.getParameter("newdPassword");
		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(GlobalConstants.SYS_USER);
		Long userId = sysUser.getUserId();
		if (customService.checkPassword(userId, oldPassword)) {
			if(customService.updatePassword(userId, newdPassword)){
				return 0;
			}
			return 1;
		}
		return 1;
	}
}
